package cn.kbblog.interceptor;

import java.util.Locale;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.View;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;


public class I18nInterceptor extends HandlerInterceptorAdapter {

	@Override
	public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler,
			ModelAndView modelAndView) throws Exception {

		Locale defaultLocale = Locale.getDefault();
//		String country = defaultLocale.getCountry();
		String language =  defaultLocale.getLanguage();
		 
		String viewName = modelAndView.getViewName();
		//如果视图文件过多，就不能采取这种方式了。使用相同的视图，把文字全部提取出来做i18n
		modelAndView.setViewName(viewName+"/"+viewName+"_"+language);
	
		super.postHandle(request, response, handler, modelAndView);
	}

	@Override
	public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
			throws Exception {
		return super.preHandle(request, response, handler);
	}

}
